Skip to main content

Working with documents

Creating a document

type MyDocument struct {
Name string `json:"name"`
Counter int `json:"counter"`
}

doc := MyDocument{
Name: "jan",
Counter: 23,
}
ctx := context.Background()
meta, err := col.CreateDocument(ctx, doc)
if err != nil {
// handle error
}
fmt.Printf("Created document with key '%s', revision '%s'\n", meta.Key, meta.Rev)

Read a document

var doc MyDocument 
ctx := context.Background()
meta, err := col.ReadDocument(ctx, "myDocumentKey (meta.Key)", &doc)
if err != nil {
// handle error
}

Read a document with an explicit revision

var doc MyDocument 
revCtx := driver.WithRevision(ctx, "mySpecificRevision (meta.Rev)")
meta, err := col.ReadDocument(revCtx, "myDocumentKey (meta.Key)", &doc)
if err != nil {
// handle error
}

Delete/Remove a document

Remove a document

ctx := context.Background()
meta, err := col.RemoveDocument(ctx, myDocumentKey)
if err != nil {
// handle error
}

Remove a document with an explicit revision

revCtx := driver.WithRevision(ctx, "mySpecificRevision")
meta, err := col.RemoveDocument(revCtx, myDocumentKey)
if err != nil {
// handle error
}

Update a document

ctx := context.Background()
patch := map[string]interface{}{
"name": "Frank",
}
meta, err := col.UpdateDocument(ctx, myDocumentKey, patch)
if err != nil {
// handle error
}
 
Help us improve

Anything unclear or buggy in this tutorial? Provide Feedback